import * as PIXI from 'pixi.js'
import '@pixi/extract'
import useMainStore from '@/store/modules/useMainStore'

export const exportCard = async (target: PIXI.ContainerChild, name: string = '五行师') => {
  const mainStore = useMainStore()
  const app = await mainStore.getApp()
  const img = await app.renderer.extract.base64(target)

  const download = document.createElement('a')
  download.href = img
  download.download = name
  download.click()
  download.remove()
}

export const exportJson = async (data: Wx.Card.Diy, name?: string) => {
  const jsonData = JSON.stringify(data, null, 2)
  const blob = new Blob([jsonData], { type: 'application/json' })
  const url = URL.createObjectURL(blob)
  const download = document.createElement('a')
  download.href = url
  download.download = (name || data.name || '五行师') + '.json'
  download.click()
  download.remove()
  URL.revokeObjectURL(url)
}
